<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登录页</title>
    <link href="/ssm/static/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<style>
    .demo-login-container{width: 320px; margin: 21px auto 0;}
    .demo-login-other .layui-icon{position: relative; display: inline-block; margin: 0 2px; top: 2px; font-size: 26px;}
    #title{
        text-align: center;
        color: #16baaa;
    }
</style>

<div style="margin-top: 200px">
    <h2 id="title">欢迎登录</h2>
    <form class="layui-form">
        <div class="demo-login-container">
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-username"></i>
                    </div>
                    <input type="text" name="username" value="admin" lay-verify="required" placeholder="用户名" lay-reqtext="请填写用户名" autocomplete="off" class="layui-input" lay-affix="clear">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-wrap">
                    <div class="layui-input-prefix">
                        <i class="layui-icon layui-icon-password"></i>
                    </div>
                    <input type="password" name="password" value="123456" lay-verify="required" placeholder="密   码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="eye">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-vercode"></i>
                            </div>
                            <input type="text" name="captcha" value="" lay-verify="required"  placeholder="验证码" lay-reqtext="请填写验证码" autocomplete="off" class="layui-input" lay-affix="clear">
                        </div>
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="/ssm/captcha" onclick="this.src='/ssm/captcha?t='+ new Date().getTime();">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="login">登录</button>
            </div>
        </div>
    </form>

</div>
<script src="/ssm/static/layui/layui.js"></script>
<script>
    layui.use(function(){
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;

        // 提交事件
        form.on('submit(login)', function(data){

            let formData = data.field; // 获取表单字段值
            // 发送异步请求  ajax 完成登录   /user/login
            $.ajax({
                url:'/ssm/user/login',  //  异步请求接口
                type:'POST',  // 请求方式
                data:formData,  //  请求参数
                dataType:'JSON', //  响应结果类型
                success:function (res) {   // 请求接口成功回调      res 已经隐式转换成了  js对象
                    if (res.code==0){

                        let {nickname,username} = res.data  //  {username,password,nickName}
                        sessionStorage.setItem("nickname",nickname)
                        sessionStorage.setItem("username",username)
                        // 跳转页面  到主页面
                        layer.msg(res.message,{icon:1},function (){
                            window.location.href = "/ssm/admin/index.html"
                        })
                    }else{
                        layer.msg(res.message,{icon:2})
                    }
                },
                error:function (){       // 请求接口失败回调
                    layer.msg('请求失败',{icon:2})
                }
            })

            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>